$(function(){
    var sub_id = location.pathname.split('/').slice(-3);
    if(isNaN(parseInt(sub_id[0])) || isNaN(parseInt(sub_id[1]))) sub_id = ['', ''];
    var url = '/tp/'+BT.context('pk')+'/calendar/events/?t='+new Date().getTime() + '&subproduct_id=' + sub_id[0] + '&price_label_type='+ sub_id[1];


    $.ajax({
        url: url,
        type: 'GET',
        data : {
            start : BT.context('first-timestamp'),
            end : BT.context('last-timestamp')
        },
        dataType: 'json',
        success: function (res) {
            if(res){

                var firstDate = BT.context('first-date'),
                    lastDate = BT.context('last-date'),
                    firstMoment = moment(firstDate),
                    lastMoment = moment(lastDate);

                var calendar = new BT.Calendar($('#calendar'),{
                    currentDate:moment(firstDate),
                    minDate:firstDate,
                    maxDate:lastDate,
                    departDatesInfo:res,//必须是在minDate和maxDate范围之内
                    displayMonthes:lastMoment.startOf('month').diff(firstMoment.startOf('month'),'month') + 1,// 同时显示的月数，默认为1
                    enableHeaderAction:true, //当显示一个月时，是否启用头部前后按钮
                    afterPickedDate: function(o){
                        var array = location.href.split('/'),
                           url = array.slice(0, array.length - 2).join('/');
                        $.get(url +"/order_url/", {
                            travel_date: o.getSelectedDay()
                        }, function(data){
                            if(data.success){
                                window.location.href = data.url;
                            } else {
                                if(data.errtype = 'tel-only'){
                                    if( BT.context('user_agent') == '1' || BT.context('user_agent') == '4' ){
                                        window.location = "tel:" + data.tel;
                                    }else{
                                        alert('电话咨询：' + data.tel);
                                    }
                                }else{
                                    alert( "抱歉,这一天不能选择,请换个日子");
                                }
                                
                            }
                        })
                    }
                });
            }
            
        },
        error: function(){
            
        }
    });
    
})
